getters.js ➔ ... ➔   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 21
ccs 3
cts 9
cp 0.3333
crap 1.2963
rs 9.3142
c 0
b 0
f 0
1 4
export const getUser = function(state) {
2
	return state.user;
3
};
4
5 4
export const isLoggedIn = function(state) {
6 4
	return state.user !== undefined && state.user !== null;
7
};
8
9
/**
10
 * Checks for the threshold if the user can post again or still has to wait
11
 *
12
 * @return boolean
13
 */
14 4
export const canPostAgain = function(state) {
15 8
	if (state.lastCommentTime === null || typeof state.config.threshold !== 'number') {
16
		return true;
17
	}
18
19
	let currentDate = new Date();
20
	let pastDate = new Date(state.lastCommentTime);
21
22
	pastDate.setSeconds(pastDate.getSeconds() + state.config.threshold);
23
24
	return currentDate > pastDate;
25
}
26
27 4
export const canPost = function(state) {
28 4
	if (state.config.loginRequired) {
29
		return isLoggedIn(state);
30
	}
31
	return true;
32
};
33
34 4
export const getConfig = function(state) {
35 2
	return function(key = null) {
36 4
		if (key === null) {
37
			return state.config;
38
		}
39
40 4
		if (typeof key !== 'string') {
41
			throw 'Config key must be a string';
42
		}
43
44
		return state.config[key];
45
	}
46
};
47
48 4
export const isMyComment = function (state) {
49 6
	return function(comment) {
50 8
		if (typeof state.user === 'object' && state.user !== null) {
51
			return state.user.id == comment.user_id;
52
		}
53
54
		return false;
55
	}
56
};
57
58 4
export const getOrderBy = function(state) {
59
	return state.orderBy;
60
};
61
62 4
export const getComments = function(state) {
63
	return state.comments;
64
};
65
66 4
export const hasChildren = function(state) {
67 6
	return function(model, modelId, parentId = null) {
68
		let result = state.comments.filter(function(comment) {
69
			// Has to be weak typed, fucking dynamic types
70 6
			return comment.model == model
71
				&& comment.foreign_key == modelId
72
				&& comment.parent_id == parentId;
73
		});
74
75
		return result.length > 0;
76
	}
77
};
78
79 4
export const hasMore = function(state) {
80 2
	return function(model, modelId, parentId = null) {
81
		let key = model + modelId + parentId;
82
		let pagination = Object.assign({}, state.pagination);
83
84 4
		if (pagination[key] !== undefined) {
85
			return pagination[key].nextPage;
86
		}
87
88
		return false;
89
	}
90
};
91
92 4
export const getCommentsForModel = function(state) {
93 2
	return function(model, modelId, parentId = null) {
94
		let comments = state.comments.filter(function(comment) {
95
			// Has to be weak typed, fucking dynamic types
96 6
			return comment.model == model
97
				&& comment.foreign_key == modelId
98
				&& comment.parent_id == parentId;
99
		});
100
101
		comments.sort((a, b) => {
102 6
			switch (state.orderBy) {
103
				case 'newest':
104
					return a.created < b.created;
105
				case 'oldest':
106
					return a.created > b.created;
107
				default:
108
					return a.created < b.created;
109
			}
110
		});
111
112
		return comments;
113
	}
114
};
115
116 4
export const getPagination = function(state) {
117 4
	return function (model, modelId, parentId = null) {
118 4
		let key = model + modelId + parentId;
119
120 4
		if (state.pagination[key] !== undefined) {
121 2
			return state.pagination[key];
122
		}
123
124 2
		return {
125
			page: 1,
126
			nextPage: false,
127
			prevPage: false,
128
			pageCount: 1
129
		}
130
	}
131
}
132